package com.iflysse.ex;

import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import com.iflysse.consts.ResultEnum;
import com.iflysse.vo.AjaxResult;

@ControllerAdvice
@ResponseBody
public class ExceptionController {

	@ExceptionHandler(value = CommonException.class)
    public Object exceptionHandler(CommonException e){
		return new AjaxResult(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = BindException.class)
    public Object exceptionHandler(BindException e){
    	return new AjaxResult(ResultEnum.NOPARAMETER.getCode(), 
    			ResultEnum.NOPARAMETER.getMsg(), 
    			e.getBindingResult().getFieldError().getDefaultMessage());
    }
    
    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    public Object exceptionHandler(MethodArgumentTypeMismatchException e){
    	return new AjaxResult(ResultEnum.NOPARAMETER.getCode(), 
    			ResultEnum.NOPARAMETER.getMsg(), 
    			"参数" + e.getPropertyName() + "类型不匹配");
    }

	@ExceptionHandler(value = Exception.class)
    public Object exceptionHandler(Exception e){
        return new AjaxResult(ResultEnum.ERROR.getCode(), 
    			ResultEnum.ERROR.getMsg());
    }
}
